#include <iostream>
#include <vector>

using namespace std;

/*
int n;

int main() {
    cin >> n;

    int arr[30][30] = { 1 };
    for (int i = 0; i < 30; i++)
    {
        for (int j = 0; j < 30; j++)
            arr[i][j] = 1;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            if (j == 0 || (i == j))
            {
                arr[i][j] = 1;
                printf("%5d", arr[i][j]);
            }
            else if (i >= 2)
            {
                int ret = arr[i - 1][j] + arr[i - 1][j - 1];
                arr[i][j] = ret;
                printf("%5d", arr[i][j]);
            }
        }
        cout << endl;
    }

}*/





class Solution {
public:
    int n, m;
    bool vis[100][100] = { 0 };
    int dx[4] = { 0, 1, 0, -1 };
    int dy[4] = { 1, 0, -1, 0 };


    bool exist(vector<string>& board, string word) {
        n = board.size();
        m = board[0].size();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (board[i][j] == word[0]) {
                    if (dfs(board, i, j, word, 0))
                        return true;
                }
            }
        }
        return false;

    }
    bool dfs(vector<string>& board, int i, int j, string word, int pos) {
        if (pos == word.size() - 1) {
            return  true;
        }

        vis[i][j] = true;

        for (int k = 0; k < 4; k++) {
            int a = i + dx[k];
            int b = j + dy[k];
            if (a >= 0 && a < n && b >= 0 && b < m && !vis[a][b] &&
                board[a][b] == word[pos + 1]) {
                if (dfs(board, a, b, word, pos + 1))
                    return true;
            }
        }

        vis[i][j] = false;
        return false;
    }
};